package com.huanxing.cloud.security.config;

import com.huanxing.cloud.security.handler.RtErrorHandler;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Primary;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

@ComponentScan("com.huanxing.cloud.common.security")
public class HxResourceServerAutoConfiguration {
  @Bean
  @Primary
  @LoadBalanced
  public RestTemplate lbRestTemplate() {
    SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
    requestFactory.setOutputStreaming(
        false); // 解决401报错时，报java.net.HttpRetryException: cannot retry due to server authentication,
                // in streaming mode
    RestTemplate restTemplate = new RestTemplate(requestFactory);
    restTemplate.setErrorHandler(new RtErrorHandler());
    return restTemplate;
  }
}
